
#include "../log/util.hpp"
#include "../log/level.hpp"
#include "../log/formatter.hpp"
#include "../log/sink.hpp"
#include "../log/logger.hpp"
#include "../log/buffer.hpp"
#include "../log/log.h"
#include <unistd.h>

// void TestGlobalBuilder()
// {
//     // 测试 8.loggerManager
//     Log::Logger::ptr logger = Log::LoggerManager::getInstance().getLogger("async_logger");
//     logger->debug(__FILE__, __LINE__, "%s", "测试同步日志");
//     logger->info(__FILE__, __LINE__, "%s", "测试同步日志");
//     logger->warn(__FILE__, __LINE__, "%s", "测试同步日志");
//     logger->error(__FILE__, __LINE__, "%s", "测试同步日志");
//     logger->fatal(__FILE__, __LINE__, "%s", "测试同步日志");
//     size_t count = 0;
//     size_t sz = 0;
//     while (sz < 1024 * 1024 * 10)
//     {
//         logger->fatal(__FILE__, __LINE__, "测试-%d", count++);
//         sz += 100;
//     }
// }

int main()
{

    // 1. 测试实用类 util
    // std::cout << Log::util::date::now() << std::endl;

    // std::cout << Log::util::file::exists("./a") << std::endl;
    // std::cout << Log::util::file::path("abc/acd/aaa") << std::endl;
    // Log::util::file::create_directory("a");
    // Log::util::file::create_directory("b/c/d.txt");
    // Log::util::file::create_directory("./abc/bcd");
    // std::cout << "dassd";

    // 2. 测试level
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::UNKNOW) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::DEBUG) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::INFO) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::WARN) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::ERROR) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::FATAL) << std::endl;
    // std::cout << Log::LogLevel::tostring(Log::LogLevel::value::OFF) << std::endl;

    // 3. 测试formatter
    // Log::LogMsg ms(10,"main.c","root","格式化测试..",Log::LogLevel::value::INFO);
    // Log::Formatter f("abcdfsdsd%%[%d{%H:%M:%S}][%t][%p][%c][%f:%l] %m%n");
    // Log::Formatter f("abcdfsdsd%%[%d{%H:%M:%S][%t][%p][%c][%f:%l] %m%n");
    // Log::Formatter f("abcdfsdsd%%[%d{%H:%M:%S}][%t][%p][%c][%f:%");
    // Log::Formatter f("abcdfsdsd%%%g[%d{%H:%M:%S}][%t][%p][%c][%f:%l] %m%n");

    // std::cout << f.format(ms) << std::endl;

    // 4. 测试sink
    // Log::LogMsg ms(10, "main.c", "root", "格式化测试..", Log::LogLevel::value::INFO);
    // Log::Formatter f;
    // std::string s = f.format(ms);
    // Log::LogSink::ptr sdsink = Log::SinkFactory::create<Log::StdOutSink>();
    // sdsink->log(s.c_str(), s.size());
    // Log::LogSink::ptr filesink = Log::SinkFactory::create<Log::FileSink>("./logfile/test.log");
    // filesink->log(s.c_str(), s.size());
    // Log::LogSink::ptr rollsize_sink = Log::SinkFactory::create<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // rollsize_sink->log(s.c_str(), s.size());

    // 4.1 测试rollsize
    // size_t count = 0;
    // while (count < 1024 * 1024 * 1000)
    // {
    //     rollsize_sink->log(s.c_str(), s.size());
    //     count += s.size();
    // }

    // 4.2 测试扩展rolltime
    // Log::LogSink::ptr rolltime_sink = Log::SinkFactory::create<Log::RollByTimeSink>("./logfile/roll-", Log::RollByTimeSink::GapTime::Second);
    // time_t old_time = Log::util::date::now();
    // while (Log::util::date::now() < old_time + 5)
    // {
    //     rolltime_sink->log(s.c_str(), s.size());
    //     usleep(1000);
    // }

    // 5. 测试logger 同步日志器
    // Log::LogSink::ptr sdsink = Log::SinkFactory::create<Log::StdOutSink>();
    // Log::LogSink::ptr filesink = Log::SinkFactory::create<Log::FileSink>("./logfile/test.log");
    // Log::LogSink::ptr rollsize_sink = Log::SinkFactory::create<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // std::vector<Log::LogSink::ptr> sinks = {sdsink, filesink, rollsize_sink};
    // Log::Formatter::ptr fmt(new Log::Formatter());
    // Log::Logger::ptr sylogger(new Log::SyncLogger(Log::LogLevel::value::ERROR, fmt, sinks, "test"));

    // sylogger->debug(__FILE__, __LINE__, "%s", "测试同步日志");
    // sylogger->info(__FILE__, __LINE__, "%s", "测试同步日志");
    // sylogger->warn(__FILE__, __LINE__, "%s", "测试同步日志");
    // sylogger->error(__FILE__, __LINE__, "%s", "测试同步日志");
    // sylogger->fatal(__FILE__, __LINE__, "%s", "测试同步日志");
    // size_t count = 0;
    // size_t sz = 0;
    // while (sz < 1024 * 1024 * 10)
    // {
    //     sylogger->fatal(__FILE__, __LINE__, "测试-%d",count++);
    //     sz += 100;
    // }

    // 5.2  测试buildlogger建造者
    // std::unique_ptr<Log::LocalLoggerBuilder> builder(new Log::LocalLoggerBuilder());
    // builder->buildloggerName("同步日志器建造者测试");
    // builder->buildsink<Log::StdOutSink>();
    // builder->buildsink<Log::FileSink>("./logfile/test.log");
    // builder->buildsink<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // Log::Logger::ptr logger = builder->build();
    // logger->debug(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试同步日志");
    // size_t count = 0;
    // size_t sz = 0;
    // while (sz < 1024 * 1024 * 10)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试-%d",count++);
    //     sz += 100;
    // }

    // 6. 测试 buffer 通过对buffer 进行读取来测试
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false)
    // {
    //     std::cerr << "ifs open fail\n";
    //     return -1;
    // }
    // // 计算出文件的大小
    // ifs.seekg(0, std::ios::end); // 读写位置跳转到文件末尾
    // size_t fsize = ifs.tellg();  // 计算出当前读写位置相对于起始位置的偏移量
    // ifs.seekg(0, std::ios::beg); // 读写位置跳转会起始位置

    // std::string s;
    // s.resize(fsize);
    // ifs.read(&s[0],fsize);
    // if(ifs.good() == false)
    // {
    //     std::cerr << "ifs read to s fail\n";
    //     return -1;
    // }
    // ifs.close();

    // Log::Buffer buff;
    // for(int i = 0; i < s.size() ; i++)
    // {
    //     buff.push(&s[i],1);
    // }

    // std::ofstream ofs("./logfile/test-buffer", std::ios::binary);
    // size_t readsize = buff.readAbleSize();
    // for(int i = 0; i < readsize; i++)
    // {
    //     ofs.write(buff.begin(),1);
    //     buff.pop(1);
    // }
    // ofs.close();

    // 7. 测试looper.hpp ,异步日志器 ,
    // std::unique_ptr<Log::LocalLoggerBuilder> builder(new Log::LocalLoggerBuilder());
    // builder->buildloggerName("异步日志器建造者测试");
    // builder->buildLoggerType(Log::BuildType::LOGGER_ASYNC);
    // builder->buildAsyncUnSafe();
    // builder->buildsink<Log::StdOutSink>();
    // builder->buildsink<Log::FileSink>("./logfile/async.log");
    // builder->buildsink<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // Log::Logger::ptr logger = builder->build();
    // logger->debug(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试同步日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试同步日志");
    // size_t count = 0;
    // size_t sz = 0;
    // while (sz < 1024 * 1024 * 10)
    // {
    //     logger->fatal(__FILE__, __LINE__, "测试-%d",count++);
    //     sz += 100;
    // }

    // 8. 测试 loggerManager 和 GlobalBuiler
    // std::unique_ptr<Log::GlobalLoggerBuilder> builder(new Log::GlobalLoggerBuilder());
    // builder->buildloggerName("async_logger");
    // builder->buildLoggerType(Log::BuildType::LOGGER_ASYNC);
    // builder->buildAsyncUnSafe();
    // builder->buildsink<Log::StdOutSink>();
    // builder->buildsink<Log::FileSink>("./logfile/async.log");
    // builder->buildsink<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // builder->build();

    // TestGlobalBuilder();

    // 9. 测试log.h 全局接口
    // Log::DEBUG("%s", "测试全局接口");
    // Log::INFO("%s", "测试全局接口");
    // Log::WARN("%s", "测试全局接口");
    // Log::ERROR("%s", "测试全局接口");
    // Log::FATAL("%s", "测试全局接口");

    std::unique_ptr<Log::GlobalLoggerBuilder> builder(new Log::GlobalLoggerBuilder());
    builder->buildloggerName("async_logger");
    builder->buildLoggerType(Log::BuildType::LOGGER_ASYNC);
    builder->buildAsyncUnSafe();
    builder->buildsink<Log::StdOutSink>();
    builder->buildsink<Log::FileSink>("./logfile/async.log");
    builder->buildsink<Log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    builder->build();


    Log::Logger::ptr logger = Log::getlLogger("async_logger");
    logger->debug("%s", "测试全局接口");
    logger->info("%s", "测试全局接口");
    logger->warn("%s", "测试全局接口");
    logger->error("%s", "测试全局接口");
    logger->fatal("%s", "测试全局接口");
    return 0;
}